﻿using UnityEngine;
using System.Collections;

public class CameraManager : MonoBehaviour
{

    // The target we are following
    public Transform target;
    // The distance in the x-z plane to the target
    // How much we
    public float heightDamping = 10;

    private float maxCameraHeight = 11;
    private float minCameraHeight = 3;

    void Start()
    {
        transform.position = new Vector3(transform.position.x, minCameraHeight, transform.position.z);
    }

    void LateUpdate()
    {
        // Early out if we don't have a target
        if (!target)
        {
            return;
        }

        // Calculate the current rotation angles
        ///float wantedRotationAngle = target.eulerAngles.y;
        float wantedHeight = target.position.y;
        float currentHeight = transform.position.y;

        // Damp the height
        currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);

        // Set the position of the camera on the x-z plane to:

        // transform.position = target.position;
        //transform.position -= currentRotation * Vector3.forward * distance;

        // Set the height of the camera
        //transform.position.y = currentHeight;
        if (currentHeight > minCameraHeight && currentHeight < maxCameraHeight)
        {
            transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
        }

        // Always look at the target
        //transform.LookAt (target);
    }
}
